package JVM;

import java.lang.ref.SoftReference;

public class SoftReferenceDemo {

    private static void softRef_memory_enough(){
        Object o1 = new Object();
        SoftReference<Object> o2 = new SoftReference<>(o1);
        o1 = null;
        System.out.println(o1);
        System.out.println(o2.get());
    }

    // -Xms5m -Xmx5m -XX:+PrintGCDetails
    private static void softRef_memory_notEnough(){
        Object o1 = new Object();
        SoftReference<Object> o2 = new SoftReference<>(o1);
        o1 = null;
        System.out.println(o1);
        System.out.println(o2.get());

        // new个大对象，开个小内存
        try{
            byte[] bytes = new byte[30 * 1024 * 1024];
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            System.out.println(o1);
            System.out.println(o2.get());
        }
    }

    public static void main(String[] args) {
//        softRef_memory_enough();
        softRef_memory_notEnough();
    }

}
